package com.beaut.project.service;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.CRMMapper;
import com.beaut.project.util.Utility;

@Transactional(rollbackFor = Exception.class)
@Service("crmService")
public class CRMService {
	@Autowired
	private CRMMapper crmMapper;
	
	public String listPageAccountingOut(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageAccountingOut(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getAccountingById(Map<String,Object> map){
		return Utility.toJson(crmMapper.getAccountingById(map));
	}
	public String listPageSupplier(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageSupplier(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageUnPurchaseSupplier(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageUnPurchaseSupplier(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageHasPurchasedSupplier(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageHasPurchasedSupplier(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getSumUnPurchaseSupplier(Map<String,Object> map){
		Object o = crmMapper.getSumUnPurchaseSupplier(map);
		if(o != null){
			return String.valueOf(o);
		}
		return "0";
	}
	public String getSumHasPurchasedSupplier(Map<String,Object> map){
		Object o = crmMapper.getSumHasPurchasedSupplier(map);
		if(o != null){
			return String.valueOf(o);
		}
		return "0";
	}
	public String getSupplierById(Map<String,Object> map){
		return Utility.toJson(crmMapper.getSupplierById(map));
	}
	public String addSupplier(Map<String,Object> map){
		crmMapper.addSupplier(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateSupplier(Map<String,Object> map){
		crmMapper.updateSupplier(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteSupplier(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		crmMapper.deleteSupplier(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String addAccounting(Map<String,Object> map){
		crmMapper.addAccounting(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateAccounting(Map<String,Object> map){
		crmMapper.updateAccounting(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteAccounting(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		crmMapper.deleteAccounting(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageCustomer(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageCustomer(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageXmWorker(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageXmWorker(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getCustomerById(Map<String,Object> map){
		return Utility.toJson(crmMapper.getCustomerById(map));
	}
	public String addCustomer(Map<String,Object> map){
		crmMapper.addCustomer(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateCustomer(Map<String,Object> map){
		crmMapper.updateCustomer(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteCustomer(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		crmMapper.deleteCustomer(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	
	public String listPageWorker(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageWorker(map);
		List<Map<String,Object>> listResult = new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			Map<String,Object> mapWorker = list.get(i);
			mapWorker.put("fb_advances_id", mapWorker.get("id"));
			Map<String,Object> mapAlreadyMoney = crmMapper.getWorkerAdvancesMoney(mapWorker);
			Map<String,Object> mapPayMoney = crmMapper.gdWorkSumMoney(mapWorker);
			if(null!=mapAlreadyMoney){
				mapWorker.put("already_money", mapAlreadyMoney.get("already_money"));
			}
			if(null!=mapPayMoney){
				mapWorker.put("worker_pay", mapPayMoney.get("worker_pay"));
			}
			listResult.add(mapWorker);
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getWorkerById(Map<String,Object> map){
		return Utility.toJson(crmMapper.getWorkerById(map));
	}
	public String addWorker(Map<String,Object> map){
		crmMapper.addWorker(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateWorker(Map<String,Object> map){
		crmMapper.updateWorker(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteWorker(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		crmMapper.deleteWorker(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageSupplierFinanceOutDetail(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageSupplierFinanceOutDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageSupplierFinanceOutLog(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageSupplierFinanceOutLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWorkerFinanceOutDetail(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageWorkerFinanceOutDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageWorkerFinanceOutLog(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageWorkerFinanceOutLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}

	public String listPageCustomerFinanceInDetail(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageCustomerFinanceInDetail(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageCustomerFinanceInLog(Map<String,Object> map){
		List<Map<String,Object>> list= crmMapper.listPageCustomerFinanceInLog(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(list));
		Utility.putSuccess(map);
		return sb.toString();
	}
}
